<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ExamineNote;
use App\Admin\Repositories\HealthNote;
use App\Constants\HealthNoteConstant;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class HealthNoteController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HealthNote(['pusher', 'member', 'belong']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $grid->column('id');
            $grid->column('pusher_name', '发布者')->display(function (){
                if ($this->pusher_id > 0) {
                    return $this->pusher->name;
                } else if ($this->member_id > 0) {
                    return $this->member->nickname;
                } else {
                    return '';
                }
            });
            $grid->column('belong_type')->using(HealthNoteConstant::TYPE_MAPS);
            $grid->column('belong.name', '关联内容');
            $grid->column('title');
            $grid->column('examine_status')->using(HealthNoteConstant::EXAMINE_STATUS_MAP)->dot(['warning', 'danger', 'success']);
//            $grid->column('relay_count');
            $grid->column('fav_count');
            $grid->column('is_show')->switch();
            $grid->column('is_hot')->switch();
            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions){
               if ($actions->row->examine_status == HealthNoteConstant::WAIT_EXAMINE) {
                   $actions->append(new ExamineNote(\App\Models\HealthNote::class));
               }

               if (!$actions->row->pusher_id) {
                   $actions->disableEdit();
               }

                $actions->append('<a style="margin-left:5px" href="' . admin_route('health_note_comment.index', ['note_id' => $actions->row->id]) . '">查看评论</a>');
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->equal('pusher_id', '发布者')->select(\App\Models\Publisher::pluck('name', 'id')->all())->width(3);
                $filter->equal('examine_status')->select(HealthNoteConstant::EXAMINE_STATUS_MAP)->width(3);
                $filter->equal('belong_type')->select(HealthNoteConstant::TYPE_MAPS)->width(3);
                $filter->between('created_at')->datetime()->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HealthNote(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('content');
            $show->field('pusher_id');
            $show->field('belong_type');
            $show->field('belong_id');
            $show->field('examine_status');
            $show->field('examine_time');
//            $show->field('relay_count');
            $show->field('fav_count');
            $show->field('is_show');
            $show->field('is_hot');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HealthNote(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->textarea('content')->required();
            $form->select('pusher_id')->options(\App\Models\Publisher::pluck('name', 'id')->all());
            $form->hidden('member_id');
            $form->select('belong_type')
                ->options(HealthNoteConstant::TYPE_MAPS)
                ->required()
                ->when(HealthNoteConstant::TYPE_bY_MERIDIAN, function (Form $form){
                    $form->select('belong_id')->options(\App\Modules\Health\Models\Meridian::where('status', 1)->pluck('name', 'id')->all())->required();
                });
//            $form->number('relay_count');
            $form->number('fav_count');
            $form->radio('is_show')->options(['否', '是'])->default(1);
            $form->radio('is_hot')->options(['否', '是'])->default(1);

            $form->saving(function (Form $form){
                if ($form->pusher_id > 0) {
                    $form->member_id = \App\Models\Publisher::find($form->pusher_id)->member->id;
                }
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
